$(document).ready(function () {

    /**
     * 获取登录状态
     */
    $.ajax({
        url: `${API}/auth/verify`,
        type: "get",
        dataType: "json",
        xhrFields: {withCredentials: true},
        success: function (result) {
            if (!$.isEmptyObject(result.jwt)) {
                //拼接cookie
                document.cookie = result.jwt.cookieName + "=" + result.jwt.token + '; domain=.' + result.jwt.cookieDomain;
            }
            $(".nav-header-regis-login").hide();
            $(".profile-img").attr("style", "background-image: url(" + result.userInfo.headportrait + ");");
            $(".user-uname").text(result.userInfo.username);
            $(".user-info").show();
            $("#comment-user").attr("style", "background-image: url(" + result.userInfo.headportrait + ");");
        }, error: function () {
            window.location = "http://www.balabala.xyz/login.html";
        }
    });

    /**
     * 获取商品详情页数据
     */
    $.get(`${API}/item/ItemDetail`, {id: GetQueryString('id')}, function (result) {
        $("#h1").text(result.item.title);
        $("#p1").text(result.item.address);
        //分割时间
        const time = result.item.time;
        const split = time.split(" - ")[0].split(".");
        const temp = split[1] + "月" + split[2] + "日";
        $("#span1").text(temp);
        $("#img1").attr("src", `${Url}` +"/images/item/" + result.item.id + ".jpeg");
        $("#div2").text(GetQueryString('count') + '张');
        $("#p2").text('￥' + result.item.price);
        $("#p3").text('￥' + result.item.price * GetQueryString('count'));
        $("#span2").text($('#p3').text());
        $("#span3").text($('#p3').text());
        $("#div3").text('下一步支付,' + $('#p3').text());
    });

    /**
     * 获取地址栏参数
     * @param name
     * @returns {string|null}
     * @constructor
     */
    function GetQueryString(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return unescape(r[2]);
        return null;
    }

});